package com.recSys.dataProcessing;

import com.recSys.util.ResourcePathHandler;

import java.io.*;
import java.util.TreeSet;

/**
 * Created by dell on 2018/1/17.
 */
public class Deal4RecFun {
    public static final String rootPath = ResourcePathHandler.getProgrameRootPath();
    static final String filePath4Fun = rootPath + "\\data\\initial\\recFun_initial.txt";
    static final String filePath4Fun_Out = rootPath + "\\data\\format\\recFun_format.txt";
    static final String filePath4Fun_train = rootPath + "\\data\\trainSet\\recFun_train.txt";
    static final String filePath4Fun_test = rootPath + "\\data\\testSet\\recFun_test.txt";
    public static int count = 0; //数据条数
    public static  TreeSet<String> cPosSet;
    public static  TreeSet<String> dPosSet;
    public static  TreeSet<String> populaceSet;
    public static  TreeSet<String> orgSet;


    /**
     * 选用线性SVM算法 处理死亡人口数据 根据对殡仪馆的选择结果为用户分类
     *
     * 输入文件为filePathForOrg sql语句查询结果：业务办理者ID_CARD前6位，逝者ID_CARD前6位、POPULACE、DEATH_CAUSE、CREATE_ORGAN
     * 该函数将输入的非数字型数字转为数字型
     * Currently libsvm supports only numerical data. You may have to change non-numerical data to numerical.
     * For example, you can use several binary attributes to represent a categorical attribute.
     */
    public static void formatDeadInfo4Fun() {
        try{
            statistic();
            int dPosStartIndex = cPosSet.size() + 1;
            int populaceStartIndex = dPosStartIndex + dPosSet.size();
            BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath4Fun)),"UTF-8"));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(filePath4Fun_Out)),"UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br1.readLine()) != null) {
                String[] values = lineTxt.split("\t");
                bw.write(orgSet.headSet(values[4]).size() + " ");
                bw.write(cPosSet.headSet(values[0]).size() + 1 + ":1 ");//计算第几个特征值为1
                bw.write(dPosStartIndex + dPosSet.headSet(values[1]).size() + ":1 ");
                bw.write(populaceStartIndex + populaceSet.headSet(values[2]).size() + ":1 ");
                bw.newLine();
            }
            br1.close();
            bw.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }

    }

    /**
     * 对数据量 特征量进行统计
     */
    public static void statistic() {
        try {
            count = 0;
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath4Fun)),"UTF-8"));
            String lineTxt = null;
            cPosSet = new TreeSet<>();
            dPosSet = new TreeSet<>();
            populaceSet = new TreeSet<>();
            orgSet = new TreeSet<>();
            while ((lineTxt = br.readLine()) != null) {
                String[] values = lineTxt.split("\t");
                cPosSet.add(values[0]);
                dPosSet.add(values[1]);
                populaceSet.add(values[2]);
                orgSet.add(values[4]);
                count++;
            }
            br.close();
            System.out.println("total count: " + count);
            System.out.println("cPosCount:"+cPosSet.size()+" dPosCount:"+dPosSet.size()+" populaceCount:"+populaceSet.size()+" orgCount:"+orgSet.size());
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }

    }

    /**
     * 切分数据集为训练集和测试集
     */
    public  static void divideData() {
        try {
            int test_count = count/8;
            System.out.println("testSet count: " + test_count);
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec("python " + rootPath + "\\tools\\subset.py " + filePath4Fun_Out
                    + " " + test_count + " " + filePath4Fun_test + " " + filePath4Fun_train);
            BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(), "GBK"));
            String line = null;
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
//        formatDeadInfo4Fun();
//        divideData();
        System.out.println(rootPath);
        statistic();
        String[] arr = (String[])Deal4RecFun.orgSet.toArray(new String[0]);
        String orgCode = arr[32];
        System.out.println(orgCode);
    }
}
